def openr(): #读取
    file = open('file2.txt', mode='r', encoding='utf-8')
    text =file.read()
    print(text)
    file.close()

def openrw():#读写
    file = open('file2.txt', mode='r+', encoding='utf-8')
    text = file.read()
    print(text)
    file.write('world')
    file.clsoe()


def openw():#写入
    file = open('file2.txt', mode='w', encoding='utf-8')
    file.wirte('hello')
    file.close()

def opena():
    file = open('file2.txt', mode='a', encoding='utf-8')
    file.write('how')
    file.close()

def read_line():
    file = open("file2.txt",encoding = 'utf-8')

    while True:
        # 读取一行内容
        text = file.read_line()
        # 判断是否读到内容,读取到文件末尾拿到的是一个空字符串
        if not text:
            break
        print(text,end="")# 每读取一行的末尾已经有了一个 `\n` 符号，所以这里用 `end=""` 去掉换行符

    file.close()

if __name__ == '__main__':
    openr()
    openrw()
    openw()
    opena()
    read_line()#txt文本在文件中